import {YD_isEmpty} from './tools.js';
export default {
	install(Vue) {
		/**
		 * 验证非空(必填)
		 * @param value: 要验证的值
		 * @param message: 提示语
		 */
		Vue.prototype.YD_checkEmpty = (value, message = '请填写相关必填项', _this) => {
		  if(!message) console.warn('请输入相关字段名的提示语');
		  if(YD_isEmpty(value) && !_this) {
		    throw message
		  }
			if(YD_isEmpty(value) && _this) {
				_this.$refs.popup.showEvent({
					type: 'error',
					message: message,
					duration: 1400
				});
				throw '必填字段为空'
			}
		}
		/**
		 * 转换日期格式
		 * @param date: Date/string/number
		 * @param format: YYYY-MM-DD || YYYY-MM-DD hh:mm:ss
		 */
		Vue.prototype.YD_dateFormat = (date, format = 'YYYY-MM-DD', char = '-') => {
			if (date === undefined || date === '') date = new Date();
      let formatMap = [
        'YYYY-MM-DD', 'YYYY-MM-DD hh:mm:ss',
        'text', 'text hh:mm:ss',
        'empty', 'YYYY-MM-DD hh:mm',
        'YYYY-MM', 'hh:mm:ss',
        'hh:mm', 'YYYYMMDD',
        'text hh:mm', 'MM/DD',
        'YYYY-MM text'
      ];
      if (formatMap.includes(date)) {
        format = date;
        date = new Date();
      }
      if (!(date instanceof Date)) {
        if (typeof date === 'number') date = new Date(date);
        else date = new Date(date.replace(/-/g, "\/"));
      }
      let YYYY = date.getFullYear();
      let MM = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1);
      let DD = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
      let hh = date.getHours() > 9 ? date.getHours() : '0' + date.getHours();
      let mm = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
      let ss = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
      switch (format) {
        case formatMap[0]: return `${YYYY}${char}${MM}${char}${DD}`; break;
        case formatMap[1]: return `${YYYY}${char}${MM}${char}${DD} ${hh}:${mm}:${ss}`; break;
        case formatMap[2]: return `${YYYY}年${MM}月${DD}日`; break;
        case formatMap[3]: return `${YYYY}年${MM}月${DD}日 ${hh}:${mm}:${ss}`; break;
        case formatMap[4]: return `${YYYY}${MM}${DD}${hh}${mm}${ss}`; break;
        case formatMap[5]: return `${YYYY}${char}${MM}${char}${DD} ${hh}:${mm}`; break;
        case formatMap[6]: return `${YYYY}${char}${MM}`; break;
        case formatMap[7]: return `${hh}:${mm}:${ss}`; break;
        case formatMap[8]: return `${hh}:${mm}`; break;
        case formatMap[9]: return `${YYYY}${MM}${DD}`; break;
        case formatMap[10]: return `${YYYY}年${MM}月${DD}日 ${hh}:${mm}`; break;
        case formatMap[11]: return `${MM}/${DD}`; break;
        case formatMap[12]: return `${YYYY}年${MM}月`; break;
      }
    }
	}
}
